<?php

/**
 * This is the model class for table "equipe".
 *
 * The followings are the available columns in table 'equipe':
 * @property integer $idEquipe
 * @property string $observation
 * @property integer $idArchitecte
 * @property integer $idTableauArchitecte
 *
 * The followings are the available model relations:
 * @property Entreprise $idArchitecte0
 * @property TableauArchitecte $idTableauArchitecte0
 * @property Entreprise[] $entreprises
 * @property Entreprise[] $entreprises1
 */
class Equipe extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'equipe';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idTableauArchitecte', 'required'),
			array('idArchitecte, idTableauArchitecte, attestationArchitecte', 'numerical', 'integerOnly'=>true),
			array('observation', 'length', 'max'=>30000),
// 			array('attestationArchitecte', 'type', 'type'=>'boolean'),
			
			// The following rule is used by search().
			array('idEquipe, idArchitecte, idTableauArchitecte', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'architecte' => array(self::BELONGS_TO, 'Entreprise', 'idArchitecte'),
			'tableauArchitecte' => array(self::BELONGS_TO, 'TableauArchitecte', 'idTableauArchitecte'),
			'associes' => array(self::MANY_MANY, 'Entreprise', 'equipe_has_associe(idEquipe, idEntreprise)'),
			'bureauxEtude' => array(self::MANY_MANY, 'Entreprise', 'equipe_has_bureauetude(idEquipe, idEntreprise)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idEquipe' => 'Id Equipe',
			'observation' => 'Observation',
			'idArchitecte' => 'Architecte',
			'idTableauArchitecte' => 'Id Tableau Architecte',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{

		$criteria=new CDbCriteria;

		$criteria->compare('idEquipe',$this->idEquipe);
		$criteria->compare('idArchitecte',$this->idArchitecte);
		$criteria->compare('idTableauArchitecte',$this->idTableauArchitecte);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Equipe the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getNumero (  ) {
		$pos = 0;
		$equipes = Equipe::model()->findAllByAttributes( 
			array('idTableauArchitecte' => $this->idTableauArchitecte),
			array('order' => 'idEquipe')
		);
		
		foreach ( $equipes as $equipe ) {
			$pos++;
			if ( $this->idEquipe == $equipe->idEquipe) {
				return $pos;
			}
		}
	}
}
